import axios, { AxiosInstance, AxiosResponse, InternalAxiosRequestConfig } from "axios";
import { getToken } from "@/utils/cookie.ts";

const request: AxiosInstance = axios.create({
  baseURL: import.meta.env.VITE_APP_BASE_API,
  timeout: 10000,
});

request.interceptors.request.use((config: InternalAxiosRequestConfig) => {
  const token: string | null  = getToken<string>();
  if (token) {
    config.headers.Authorization = token;
  }
  return config;
}, (error) => {
  return Promise.reject(error);
});

request.interceptors.response.use((response: AxiosResponse) => {
  return response.data;
}, (error) => {
  return Promise.reject(error);

});

export default request;
